
angular.module('app').controller('addressCtrl',
    ['$scope', '$rootScope', '$stateParams','$state','$http', 'utils','activity','$location',
        function ($scope, $rootScope, $stateParams,$state,$http,utils, activity,$location) {

            $scope.goback=function () {
                window.history.go(-1);
            };
            $scope.url=$location.url();
            if($scope.isapp){
                appload={
                    CallBack:function (type) {
                        if(type==2){
                            $scope.edAddress();
                        }
                    }
                };
            }

            $scope.$on('$viewContentLoaded', function(event) {
                    if('/integral/address'==$scope.url){
                            if($rootScope.isapp) {
                                wv.setHeader('我的收货地址','0','-','0')
                            }

                    }else {
                            if($rootScope.isapp) {
                                wv.setHeader('选择收货地址','1','管理','2')
                            }
                    }
            });

            /**
             * 获取地址列表
             */
            addressInfo(activity.getUser_code());
            /**
             * 添加地址
             */
            $scope.toAdd=function () {
                $state.go('address_add')
            };

            /**
             * 选择默认图片切换
             * @param isDefault
             * @returns {*}
             */
            $scope.selectPath=function (isDefault) {
                if(!isDefault){
                    return 'image/integral/selected.png';
                }else {
                    return 'image/integral/no_select.png';
                }
            };
            /**
             * 设置默认的地址
             * @param selectAddress
             */
            $scope.setDefault=function (selectAddress) {
                if(!selectAddress.isDefault)return
                $http.get(utils.integralSys, {
                    params: {
                        header: utils.header("300450"),
                        body: {
                            addressId: selectAddress.addressId, //收货详细地址
                            userCode:activity.getSoloCode(),
                        }
                    }
                }).then(function (res) {
                    if(res.header.re_code == "0000") {
                        for (var i = 0; i < $scope.myAddressList.length; i++) {
                            if(selectAddress.addressId==$scope.myAddressList[i].addressId){
                                $scope.myAddressList[i].isDefault=0;
                            }else {
                                $scope.myAddressList[i].isDefault=1;
                            }
                        }
                    }
                });
            };

            /**
             * 设置产品选择的地址
             * @param res
             */
            $scope.setProduct=function (res) {
                activity.addressInfoDeafult=res;
                window.history.go(-1);
            };

            /**
             * 编辑地址
             * @param selectAddress
             */
            $scope.edit=function (selectAddress) {
                $state.go('address_add',{editedAddress:{
                    consignee:selectAddress.consignee,
                    phone:selectAddress.phone,
                    addressPCA:selectAddress.provinceId+' '+selectAddress.cityId+' '+selectAddress.areaId,
                    address:selectAddress.address,
                    addressId:selectAddress.addressId,
                    provinceCode:selectAddress.provinceCode,
                    cityCode:selectAddress.cityCode,
                    areaCode:selectAddress.areaCode,
                    isDefault:selectAddress.isDefault,
                }});
            };

            /**
             * 删除地址
             * @param selectAddress
             */
            $scope.deleteAddress=function (selectAddress) {
                var btnArray = ['否', '是'];
                mui.confirm('确认要删除此收货地址吗?', '', btnArray, function(e) {
                    if (e.index == 1) {
                        $http.get(utils.integralSys, {
                            params: {
                                header: utils.header("300420"),
                                body: {
                                    addressId: selectAddress.addressId, //收货详细地址
                                    status:'1',
                                    userCode:activity.getSoloCode(),
                                }
                            }
                        }).then(function(res) {
                            if(res.header.re_code == "0000") {
                                    if($scope.myAddressList){
                                        for (var i = 0; i < $scope.myAddressList.length; i++) {
                                            if(selectAddress.addressId==$scope.myAddressList[i].addressId){
                                                $scope.myAddressList.splice(i,1);
                                            }
                                        }
                                    }
                                if(!selectAddress.isDefault){
                                    if($scope.myAddressList.length>0){
                                        $scope.setDefault($scope.myAddressList[0])
                                    }
                                }
                                utils.msg('删除成功');
                            }
                        })

                    }
                })
            };

            /***
             * 切换管理页面
             */
            $scope.edAddress=function  () {
                $state.go('address')
            };


            /**
             * 地址列表
             * @param user_code
             */
            function addressInfo(user_code) {
                if(user_code){
                    $http.get(utils.integralSys,{
                        params:{
                            header:utils.header('300440'),
                            body: {
                                phoneNumber: '',
                                userCode: activity.getSoloCode(),

                            }

                        }
                    }).then(function (res) {
                        if(res.body.rows){//有收货地址
                            if(res.body.rows.length>0){
                                $scope.myAddressList=res.body.rows;
                            }else {
                                $scope.emptyList=true;
                            }
                        }else {
                            $scope.emptyList=true;
                        }
                    })
                }
            }
}]);